import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";

NProgress.configure({ showSpinner: false });

Vue.use(VueRouter);

const systemRoutes = [
  {
    path: "/login",
    name: "login",
    meta: {},
    component: () => import("../views/index/login.vue")
  }
];

const routes = [
  {
    path: "/",
    name: "root",
    redirect: "/describe",
    component: () => import("../views/layout/Layout.vue"),
    meta: { name: "", icon: "message", disabled: false },
    children: [
      {
        path: "/scan",
        name: "scan-in",
        meta: {
          name: "扫码入园",
          icon: "solution",
          disabled: false,
          sign: "1"
        },
        component: () => import("../views/scanGarden/scan.vue")
      },
      {
        path: "/waterPay",
        name: "water-pay",
        meta: {
          name: "供水缴费",
          icon: "solution",
          disabled: false,
          sign: "1"
        },
        component: () => import("../views/waterPay/index.vue")
      },
      {
        path: "/result",
        name: "scan-result",
        meta: {
          name: "扫码成功",
          icon: "solution",
          disabled: false,
          sign: "0"
        },
        component: () => import("../views/scanGarden/result.vue")
      },
      {
        path: "/describe",
        name: "describe",
        meta: {
          name: "一码通介绍",
          icon: "file-done",
          disabled: false,
          sign: "0"
        },
        component: () => import("../views/index/describe.vue")
      },
      {
        path: "/voucher",
        name: "voucher",
        meta: {
          name: "办事凭证",
          icon: "file-done",
          disabled: true,
          sign: "1"
        },
        component: () => import("../views/serviceVoucher/index.vue")
      },
      {
        path: "/voucher/result",
        name: "voucher-result",
        meta: { name: "业务办理结果", icon: "", disabled: false, sign: "0" },
        component: () => import("../views/serviceVoucher/result.vue")
      },
      {
        path: "/water/bill",
        name: "water-bill",
        meta: { name: "业务办理结果", icon: "", disabled: false, sign: "0" },
        component: () => import("../views/waterPay/bill.vue")
      },
      {
        path: "/water/scan",
        name: "water-scan",
        meta: { name: "业务办理结果", icon: "", disabled: false, sign: "0" },
        component: () => import("../views/waterPay/scan.vue")
      },
      {
        path: "/water/result",
        name: "water-result",
        meta: { name: "业务办理结果", icon: "", disabled: false, sign: "0" },
        component: () => import("../views/waterPay/result.vue")
      }
    ]
  }
];

function genMenus(routes) {
  let menus = [];
  for (let i in routes) {
    console.log("routes", routes[i]);
    const route = routes[i];
    if (route.children && route.children.length > 0) {
      if (route.meta.sign !== "0") {
        menus.push({
          name: route.meta.name,
          path: route.path,
          icon: route.meta.icon,
          disabled: route.meta.disabled,
          subMenus: genMenus(route.children)
        });
      }
    } else {
      if (route.meta.sign !== "0") {
        menus.push({
          name: route.meta.name,
          path: route.path,
          icon: route.meta.icon,
          disabled: route.meta.disabled
        });
      }
    }
  }
  return menus;
}

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes: systemRoutes.concat(routes)
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const requiresAuth = "";
    if (requiresAuth) {
      next();
    } else {
      next({
        path: "/"
      });
    }
  } else {
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
export const menus = genMenus(routes);
console.log("menu", menus);
